package com.sdp.th.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.th.cache.ThCleanCache;
import com.sdp.th.db.th_room.service.IThRoomService;
import com.sdp.th.entity.ThRoom;

/**
 * <p>
 * 配电室信息 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2025-04-12
 */
@RestController
@RequestMapping("/ThRoomController")
public class ThRoomController {

	@Autowired
	private IThRoomService iThRoomService;

	@Autowired
	private Query query;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@Autowired
	private ThCleanCache thCleanCache;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iThRoomService, ThRoom.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody ThRoom room) {
		if(room==null) {
			return Result.error("配电室信息为空");
		}

		if(StringUtils.isEmpty(room.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iThRoomService, ThRoom.class, new String[] {"roomNo","roomName"}, "keyId", room, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("配电室编码和名称不能重复");
			}
			iThRoomService.save(room);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iThRoomService, ThRoom.class, new String[] {"roomNo","roomName"}, "keyId", null, room, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("配电室编码和名称不能重复");
			}
			iThRoomService.updateById(room);
		}

		thCleanCache.cleanRoomCache();
		return Result.ok();
	}
}

